package com.ruoyi.app.controller;

import com.ruoyi.app.interceptor.JWTUtil;
import com.ruoyi.app.interceptor.SessionUtil;
import com.ruoyi.cms.domain.CrmMember;
import com.ruoyi.cms.service.ICrmMemberService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.md5.MD5;
import com.ruoyi.employee.domain.CrmEmployee;
import com.ruoyi.employee.service.ICrmEmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 客户员工Controller
 * 
 * @author hct
 * @date 2022-02-11
 */
@RestController
@RequestMapping("/app/employee")
public class AppEmployeeController extends BaseController
{
    @Autowired
    private ICrmEmployeeService crmEmployeeService;
    @Autowired
    private ICrmMemberService memberService;

    /**
     * 查询客户员工列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CrmEmployee crmEmployee)
    {
        startPage();
        List<CrmEmployee> list = crmEmployeeService.selectCrmEmployeeList(crmEmployee);
        return getDataTable(list);
    }


    /**
     * 获取客户员工详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(crmEmployeeService.selectCrmEmployeeById(id));
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/my")
    public AjaxResult myDetail(HttpServletRequest request) {
        String token = JWTUtil.getRequestToken(request);
        String phoneNumber=JWTUtil.getSubject();
        String id=phoneNumber.split("_")[0];
        CrmEmployee crmEmployee = crmEmployeeService.selectCrmEmployeeById(Long.valueOf(id).longValue());
        return AjaxResult.success(crmEmployee);
    }

    /**
     * 修改密码
     * @param password
     * @param repassword
     * @return
     */
    @PostMapping("/updatePassword")
    @ResponseBody
    public AjaxResult updatePassword(String password,String repassword,HttpServletRequest request)
    {

        if(StringUtils.isEmpty(password)){
            return AjaxResult.error("未填写，登陆密码");
        }
        if(!repassword.equals(password)){
            return AjaxResult.error("两次输入密码不一致");
        }
        CrmMember crmEmployee = SessionUtil.getLoginUser();
        if(crmEmployee!=null && crmEmployee.getId()>0){
            // 修改密码，对密码进行加密
            String passwordmd5= MD5.md5Hex(password);
            crmEmployee.setPassword(passwordmd5);
            memberService.updateCrmMember(crmEmployee);
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }

    }

    /**
     * 编辑用户信息

     * @return
     */
    @PostMapping("/updateMember")
    @ResponseBody
    public AjaxResult updateMember(String nickname,Integer sex,String headimg,HttpServletRequest request)
    {

        // 生成令牌
        CrmMember crmEmployee = SessionUtil.getLoginUser();
        if(crmEmployee!=null && crmEmployee.getId()>0){
            crmEmployee.setName(nickname);
            crmEmployee.setSex(sex+"");
            crmEmployee.setHead(headimg);
            memberService.updateCrmMember(crmEmployee);
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }

    }



}
